const PENDING=0;
const SUCCESS=1;
const FAILURE=-1;
class MyPromise {
    constructor(handle){
        this._resolvelist=[];
        this._rejectlist=[];
        this.state=PENDING;

        try {
            let _s=this._resolve.bind(this);
            let _r=this._reject.bind(this);
            //延迟handle加载到.then入队列之后,或者可以将_resolve放入setTimeout。
            setTimeout( ()=> {
                handle(_s,_r);
            },0)
        }catch (e) {
            this._reject(e);
        }
    }

    then(fn){
        this._resolvelist.push(fn);
        return this;
    }

    _resolve(val){
        let _val=val;
        this._resolvelist.forEach(resolve=>{
            _val=resolve(val);
        });

        /*for(let i=0;i<this._resolvelist.length;i++){
            let resolve=this._resolvelist[i];
            _val=resolve(val);
        }*/
    }
    _reject(val){
        throw new Error(val);
    }
}


let ff=function (resolve, reject) {
    console.log("into promise");
    //调动
    resolve(1)
};
new MyPromise(ff).then(function (val) {
    console.log("then1");
    console.log(val);
}).then(function (val) {
    console.log("then2");
    console.log(val);
})